package com.pms.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

import com.pms.entity.Ideas;

public class IdeaDao {
	private DBHelp<Ideas> db = new DBHelp<Ideas>();
	public void save(Ideas idea) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO `t_idea` ");
		sql.append("(`title`,`note`,`t_project_proid`,`t_user_useid`) ");
		sql.append("VALUES ");
		sql.append("(?,?,?,?)");
		
		db.executeSQL(sql.toString(),idea.getTitle(),idea.getNote(),idea.getProid(),idea.getUserid());
	}
	public List<Ideas> findByproid(String proid) {
		String sql="SELECT t_idea.id,t_idea.title,t_idea.note,t_idea.createtime,t_user.username FROM t_idea LEFT JOIN t_user ON t_user.useid=t_idea.t_user_useid WHERE t_idea.t_project_proid=?";
		System.out.println("sql");
		return db.executeQueryToList(sql,new RowMapper<Ideas>(){

				@Override
				public Ideas mapRow(ResultSet rs) throws SQLException {
					Ideas idea = new Ideas();
				    idea.setIdeaid(rs.getString("id"));
				    idea.setTitle(rs.getString("title"));
				    idea.setNote(rs.getString("note"));
				    idea.setCreatetime(rs.getString("createtime"));
				    idea.setUsername(rs.getString("username"));
				    
					return idea;
				}
			}, proid);
	}
	public Ideas findByid(String ideaId) {
		String  sql="SELECT id,title,note,createtime,t_project_proid,t_user_useid FROM t_idea WHERE id=?";
		return db.executeQueryToObject(sql, new IdeasRowMapper(), ideaId);
	}
	
	private class IdeasRowMapper implements RowMapper<Ideas>{
			
			public Ideas mapRow(ResultSet rs) throws SQLException {
				Ideas idea = new Ideas();
				idea.setIdeaid(rs.getString("id"));
				idea.setCreatetime(rs.getString("createtime"));
				idea.setTitle(rs.getString("title"));
				idea.setNote(rs.getString("note"));
				idea.setUserid(rs.getString("t_user_useid"));
				idea.setProid(rs.getString("t_project_proid"));
				return idea;
			}
	
		}

	public void update(String idid, String title, String note) {
		String sql="UPDATE t_idea SET title=?,note=? WHERE id=?";
		
		db.executeSQL(sql,title,note,idid);
	}
	public void del(String id) {
		String sql="DELETE FROM t_idea WHERE id=?";
		db.executeSQL(sql,id);
		
	}
	
	
	public Ideas findid(String idid) {
		String sql="SELECT t_idea.id,t_idea.title,t_idea.note,t_idea.createtime,t_user.username FROM t_idea LEFT JOIN t_user ON t_user.useid=t_idea.t_user_useid WHERE t_idea.id=?";
		System.out.println("sql");
		return db.executeQueryToObject(sql,new RowMapper<Ideas>(){

				@Override
				public Ideas mapRow(ResultSet rs) throws SQLException {
					Ideas idea = new Ideas();
				    idea.setIdeaid(rs.getString("id"));
				    idea.setTitle(rs.getString("title"));
				    idea.setNote(rs.getString("note"));
				    idea.setCreatetime(rs.getString("createtime"));
				    idea.setUsername(rs.getString("username"));
				    
					return idea;
				}
			},idid);
	}
	
	
	
}
